package com.gccloud.tgyfirst.controller.demo;

import com.gccloud.tgyfirst.demo.dto.TodoDTO;
import com.gccloud.tgyfirst.demo.entity.TodoEntity;
import com.gccloud.tgyfirst.service.demo.service.ITodoService;
import com.gccloud.tgyfirst.demo.vo.TodoVO;

import com.gccloud.starter.common.constant.GlobalConst;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.controller.SuperController;
import com.gccloud.starter.core.dto.RepeatDTO;
import com.gccloud.starter.mybatis.page.PageVO;
import com.gccloud.starter.plugins.validator.ValidatorUtils;
import com.gccloud.starter.plugins.validator.group.Insert;
import com.gccloud.starter.plugins.validator.group.Update;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 大家听我说，这个是对外开放的接口不难理解吧。
 * 注意：
 * ----1、该控制器需要集成 SuperController（因为他给你写好了几个方法，省去了你多敲几个单词，不香吗!!!）
 *
 * @Author qianxing
 * @Date 2020年11月18日22:30:40
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/todo")
/**
 * 定义在swagger中展示待办事项模块，并且它的接口排序为 10
 */
@Api(tags = "待办事项")
@ApiSort(value = 10)
public class TodoController extends SuperController {

    @Autowired
    private ITodoService todoService;

    /**
     * 分页条件查询
     *
     * @param searchDTO
     * @return
     */
    @GetMapping("/")
    /**
     * 这里的 position 定义了这个方法对应的接口在该类的所有接口中的排序为10
     */
    @ApiOperation(value = "列表", position = 10, notes = "分页查询列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<TodoVO>> getPage(@ApiParam(name = "查询", value = "传入查询的业务条件", required = true) SearchDTO searchDTO) {
        PageVO<TodoEntity> page = todoService.getPage(searchDTO);
        PageVO PageVO = BeanConvertUtils.convert(page, PageVO.class);
        PageVO.setList(page.getList());
        return success(PageVO);
    }

    /**
     * 根据id获取待办事项详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "详情", position = 20, notes = "待办详情", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/{id}")
    public R<TodoVO> getById(@ApiParam(name = "待办事件ID", value = "传入String格式", required = true) @PathVariable("id") String id) {
        TodoEntity todoEntity = todoService.getByIdWithDp(id);
        TodoVO todoVO = BeanConvertUtils.convert(todoEntity, TodoVO.class);
        return success(todoVO);
    }

    /**
     * 新增待办事项
     *
     * @param todoDTO
     * @return
     */
    @PostMapping
    @RequiresPermissions("todo:add")
    @ApiOperation(value = "新增", position = 30, notes = "新增待办事件", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> add(@ApiParam(name = "新增待办事件JSON对象", value = "传入json格式", required = true) @RequestBody TodoDTO todoDTO) {
        ValidatorUtils.validateEntity(todoDTO, Insert.class);
        TodoEntity todoEntity = BeanConvertUtils.convert(todoDTO, TodoEntity.class);
        todoService.add(todoEntity);
        return success(todoEntity.getId());
    }

    /**
     * 修改待办事项
     *
     * @param todoDTO
     * @return
     */
    @PutMapping
    @RequiresPermissions("todo:update")
    @ApiOperation(value = "修改", position = 40, notes = "修改待办事件", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> update(@ApiParam(name = "修改待办事件JSON对象", value = "传入json格式", required = true) @RequestBody TodoDTO todoDTO) {
        ValidatorUtils.validateEntity(todoDTO, Update.class);
        TodoEntity todoEntity = BeanConvertUtils.convert(todoDTO, TodoEntity.class);
        todoService.update(todoEntity);
        return success();
    }

    /**
     * 根据id删除待办事项
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("todo:delete")
    @ApiOperation(value = "删除", notes = "删除待办事件", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteById(@ApiParam(name = "待办事件ID", value = "传入String格式", required = true) @PathVariable("id") String id) {
        todoService.deleteById(id);
        return success();
    }

    /**
     * 待办事项名称查重
     *
     * @param repeatDTO
     * @return
     */
    @PostMapping("/repeat")
    @ApiOperation(value = "查重", notes = "查重", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Boolean> repeat(@ApiParam(name = "查重", value = "传入JSON对象", required = true) @RequestBody RepeatDTO repeatDTO) {
        ValidatorUtils.validateEntity(repeatDTO);
        boolean repeat = todoService.repeat(repeatDTO.getId(), repeatDTO.getName(), repeatDTO.getValue(), GlobalConst.RepeatStrategy.CREATE_BY);
//        boolean repeat = todoService.repeat(repeatDTO.getId(),
//                repeatDTO.getName(), repeatDTO.getSex(),repeatDTO.getAge(),
//                repeatDTO.getNote(),repeatDTO.getValue(), GlobalConst.RepeatStrategy.CREATE_BY);
        return success(repeat);
    }
}
